from pyexpat import features

import numpy as np
from numpy.ma.core import asarray
import matplotlib.pyplot as plt
import numpy as np

# 创建三维数组
b = np.array([
            [[1, 2, 3], [4, 5, 6], [7,8,9]],
            [[10, 20, 30],[40, 50, 60], [70,80,90]]
              ])
print(b)
print("shape",b.shape,b.dtype)
print("slice")
print(b[1::2,::2,:])

# 生成全零数组
zeros = np.zeros((3, 3,4))
print(zeros,zeros.shape)

# 生成全一数组
ones = np.ones((2, 4))
print(ones)

# 生成随机数数组
randoms = np.random.rand(3, 3)
print(randoms)

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

# 加法
print(x + y)  # 输出: [5 7 9]

# 乘法
print(x * y)  # 输出: [ 4 10 18]

# 矩阵运算
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("多维矩阵相乘",np.dot(A, B))  # 矩阵乘法

# slicing 等差数列
# fancy indexing 提供列表
#asarray(0,80,1)
y=A[1]
print("fancy indexing:%{y}")
# boolean indexing 等长的布尔数组

x=np.array([1,3,3,5,-2,-1,-90])

bool_index=(x>0)
print(bool_index)
p_x=x[bool_index]
print("boolean indexing:",p_x)


x=np.array(["zhangsan","lisi","wangwu","gebi"])
y=np.array([[60,70,80],
[62,50,90],
[60,74,80],
[90,70,89]
            ])
# 查询出zhangsan
bool_index=(x=="zhangsan")
print(bool_index)
# 查询姓名为zhangsan的成绩
result=y[x=="zhangsan"]
print("boolean indexing:",result)
# 类型转换
float_arr=result.astype(np.float64)
print("类型转换:%{}",float_arr)
# l=input()
# print("input",l)

# array broadcasting
# 特征值一定是方阵
features=np.array([[4, -2], [1, 1]])
d1=features.dot(features.T)
d2=features.T.dot(features)
# 计算特征值和特征向量（会对方阵）
eigenvalues1, eigenvectors1 = np.linalg.eig(features)
eigenvalues2,eigenvectors2=np.linalg.eig(d2)
#eigenvalues (特征值)：一维数组，包含所有特征值

# eigenvectors (特征向量)：二维数组，每一列是对应特征值的特征向量

print("eigenvalues1:",np.round(eigenvalues1,4),"eigenvalues2:",eigenvalues2)
print("eigenvectors1:",np.round(eigenvectors1,4),"eigenvectors2:",eigenvectors2)
features=np.array([[1, 2], [3, 4],[3, 4],[7, 8],[-20,-90]])
# u特征值，vt特征向量，s奇异值 （针对任意矩阵）
u,s,vt=np.linalg.svd(features)
print("s奇异值",s)
# sq=np.arange(0,s.size)
# plt.bar(sq,s)
# plt.plot(sq,s)
# plt.title("正弦函数曲线")  # 标题
# plt.xlabel("X轴")  # x轴标签
# plt.ylabel("Y轴")  # y轴标签
# plt.show()